package tsing.javase.thread.juc;

import java.util.concurrent.*;

public class MemberService {
    private ThreadPoolExecutor memberPool;

    // 构造方法中初始化线程池
    public MemberService() {
        // 创建一个可复用的会员池
        memberPool = new ThreadPoolExecutor(
                5, // 核心线程数
                10, // 最大线程数
                60, // 空闲线程存活时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue<>(100), // 任务队列
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
    }
    public void addMember(String name) {
        // 提交任务到会员池中执行
        memberPool.execute(() -> {
            System.out.println(name + " is joining the club");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " has joined the club");
        });
    }

    public void removeMember(String name) {
        // 提交任务到会员池中执行
        memberPool.execute(() -> {
            System.out.println(name + " is leaving the club");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " has left the club");
        });
    }

    public static void main(String[] args) {

        MemberService memberService = new MemberService();


    }
}
